package com.assessment.service.impl;

import com.assessment.entity.*;
import com.assessment.mapper.AdepMapper;
import com.assessment.mapper.ApeoMapper;
import com.assessment.mapper.AroleMapper;
import com.assessment.mapper.AuserMapper;
import com.assessment.service.AdepService;
import com.assessment.service.ApeoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.assessment.utils.Constant.COMMON;
import static com.assessment.utils.Constant.PWD_HEAD;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@Service
@Transactional
public class ApeoServiceImpl extends ServiceImpl<ApeoMapper, Apeo> implements ApeoService {
    @Autowired(required = false)
    private AroleMapper aroleMapper;
    @Autowired(required = false)
    private AdepMapper adepMapper;
    @Autowired(required = false)
    private AuserMapper auserMapper;

    @Override
    public List<Column> getColumns() {
        List<Column> columns = new ArrayList<>();
        columns.add(new Column().setLabel("工号").setProp("pid"));
        columns.add(new Column().setLabel("姓名").setProp("name"));
        columns.add(new Column().setLabel("所属部门").setProp("dname"));
        columns.add(new Column().setLabel("角色").setProp("rname"));
        return columns;
    }

    @Override
    public boolean checkPeo(Apeo data) {
        // 人员是不是重复
        QueryWrapper<Apeo> apeoQueryWrapper = new QueryWrapper<>();
        apeoQueryWrapper.eq("name", data.getName());
        apeoQueryWrapper.eq("pid", data.getPid());
        List<Apeo> list = this.list(apeoQueryWrapper);
        if (list.size() > 0) { return false; }

        // 部门是不是存在
        QueryWrapper<Adep> adepQueryWrapper = new QueryWrapper<>();
        adepQueryWrapper.eq("name", data.getDname());
        List<Adep> adeps = adepMapper.selectList(adepQueryWrapper);
        if (adeps.size() <= 0){ return false; }

        // 角色是不是存在
        QueryWrapper<Arole> aroleQueryWrapper = new QueryWrapper<>();
        aroleQueryWrapper.eq("name", data.getRname());
        List<Arole> aroles = aroleMapper.selectList(aroleQueryWrapper);
        if (aroles.size() <= 0){ return false; }

        data.setDid(adeps.get(0).getId());
        data.setDrid(adeps.get(0).getRid());
        data.setDrole(adeps.get(0).getRname());
        data.setDsit(adeps.get(0).getDsit());
        data.setRid(aroles.get(0).getId());
        data.setRtype(aroles.get(0).getType());
        data.setRsit(aroles.get(0).getRsit());

        return true;
    }

    @Override
    public boolean saveUser(Apeo item) {
        this.save(item);
        Auser auser = new Auser().setSprole(COMMON).setAccount(item.getPid()).setPwd(PWD_HEAD+item.getPid()).setUid(item.getId()).setName(item.getName());
        auserMapper.insert(auser);
        return true;
    }

    @Override
    public void saveUserBatch(List<Apeo> list) {
        list.forEach(item -> {
            Auser auser = new Auser().setSprole(COMMON).setAccount(item.getPid()).setPwd(PWD_HEAD+item.getPid()).setUid(item.getId()).setName(item.getName());
            auserMapper.insert(auser);
        });
    }
}
